<?php
class Emergency_model extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
    /**
	 * 获取用户信息
	 * @param int $id
	 */
	public function find_by_id($id) {
		$sql = "select u.nickname,u.email,u.is_vertification,e.* from emergencies e left join users u on 
		 u.id=e.created_by where e.id=" . intval($id);
		return $this->db->query($sql)->row_array();
	}
	
	public function add($data) {
		if (!empty($data['id'])) {
			$story = $this->find_by_id($data['id']);
			$this->db->where('id', $data['id']);
			$this->db->update('emergencies', $data); 
			if ($data['category_id'] != $story['category_id']) {
				$sql = "update categories set emergency_count=emergency_count+1 where id=" . intval($data['category_id']);
				$this->db->query($sql);
				$sql = "update categories set emergency_count=emergency_count-1 where id=" . intval($story['category_id']);
				$this->db->query($sql);
			}
			return $data['id'];
		} else {
			$this->db->insert("emergencies", $data);
			$new_story_id = $this->db->insert_id();
			$sql = "update categories set emergency_count=emergency_count+1 where id=" . intval($data['category_id']);
			$this->db->query($sql);
			return $new_story_id;
		}
	}
	/**
	 * story相关图片
	 * @param int $id
	 * @param array $pic_data
	 */
	public function add_pic($id, $pic_data) {
		$sql = " insert into upload_pics set target_type='story', target_id=" . $id .", path=" .$this->db->escape($pic_data['file_name']) .
		" ,thumb_path=" . $this->db->escape($pic_data['raw_name'] . "_thumb" . $pic_data['file_ext'])
		. ",pic_type=" . $this->db->escape($pic_data['file_type']);
		return $this->db->query($sql);
	}
	
	public function get_count_by_category_id ($category_id) {
		$sql = " select count(*) as total from emergencies g  where g.status=1 and g.category_id=" . $category_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	public function find_by_category_id ($category_id, $offset = 0, $limit = 10) {
		$sql = " select g.*,u.nickname from emergencies g left join users u on g.created_by=u.id where g.status=1 and  g.category_id=" . $category_id;
		$sql .= " order by g.created desc limit " . $offset . ", " . $limit;
		return $this->db->query($sql)->result_array();
	}
	
	public function get_count_by_condition($condition) {
		$sql = " select count(*) as total from emergencies g  where " . $condition;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	public function find_by_condition ($condition, $offset = 0, $limit = 10) {
		$sql = " select g.*,u.nickname from emergencies g left join users u on g.created_by=u.id where " . $condition;
		$sql .= " order by g.created desc limit " . $offset . ", " . $limit;
		
		return $this->db->query($sql)->result_array();
	}
	
	public function update_status($status, $id) {
		$this->db->where('id', $id);
		return $this->db->update('emergencies', array('status' => $status)); 
	}
	
	public function delete($id) {
		$this->db->where('id', $id);
		$this->db->delete('emergencies'); 
	}
	
	public function top($id, $days, $top_by_admin = 0) {
		$sql = " update emergencies set is_top=1, top_by_admin = " . $top_by_admin . ", top_start_time='" . date("Y-m-d H:i:s") . "', top_end_time='" . date("Y-m-d H:i:s",time()+$days*24*3600) . "'"
		. " where id=" . intval($id);
		return $this->db->query($sql);
	}
	
	public function untop($id) {
		$sql = " update emergencies set is_top=0, top_by_admin = 0, top_start_time=null, top_end_time=null"
		. " where id=" . intval($id);
		return $this->db->query($sql);
	}
	public function get_count_by_condition_search($sql) {
		//print $sql;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	public function find_by_condition_search($sql, $offset = 0, $limit = 10) {
		//$sql .= "order by g.created desc ";
		$sql .= " limit " .$offset . "," . $limit;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	
	public function find_admin_top() {
		$sql = "select e.id, e.created_by,e.title,e.top_start_time,u.nickname from emergencies e
		left join users u on u.id= e.created_by
		where e.is_top=1 ";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
}